/**
\file Timer.cpp
\author Zachary Wartell - Copyright 2007.  All rights reserved.
\brief OpenGLTrainer.cpp implements various functions to help ITCS4120 students get up 
and running quickly in the first few programming assignments.

This functions in this file can generally just be used as given in the per project 
skeleton code without students being concerned with how these functions work.  We will
cover all this stuff eventually in later sections of the course.

More detailed comments go here.  

TO DO LIST:
- \\ todo Replace the '\\ ' with '\' to generate a real todo comment

BUG LIST:
- \\ bug Replace the '\\ ' with '\' to generate a real bug comment. 

FOOTNOTES:
- [_F1_] <blank example. replace _F1_ with F1,F2, etc. for actual footnote>

REFERENCES:
- \anchor DK90 Donald Knuth, Algorithms and Data Structures, 1990.

\internal
*/

/*******************************************************************************
    Includes	
*******************************************************************************/
#include <OpenGLTrainer\Timer.h>
#include <cassert>

using namespace ITCS4120;
/*******************************************************************************
    Private Macros 
*******************************************************************************/
/* NONE */
/*******************************************************************************
    Private Function Prototypes 
*******************************************************************************/
/* NONE */
/*******************************************************************************
    Private Data Types 
*******************************************************************************/
/* NONE */


/*******************************************************************************
    Private Global Declarations 
*******************************************************************************/
/* NONE */
/*******************************************************************************
    Internal Global Declarations 
*******************************************************************************/
/* NONE */
/*******************************************************************************
    Exported Global Declarations 
*******************************************************************************/
/* NONE */


/*******************************************************************************
    Exported Functions & Exported Class Member Functions 
*******************************************************************************/
#ifdef _WIN32
LARGE_INTEGER OpenGLTrainer::Timer::frequency={0};
#endif

OpenGLTrainer::Timer::Timer()
    {
   #ifdef _WIN32
    if (!frequency.QuadPart)
	//assert_always(QueryPerformanceFrequency(&frequency));
	assert(QueryPerformanceFrequency(&frequency));
    QueryPerformanceCounter(&start_);
    lastLap_ = start_;
   #else
    lastLap_ = clock();
   #error Unix Version Not fully implemented yet!
   #endif
    }

/**
\brief reset timer to 0
*/
void OpenGLTrainer::Timer::reset()
    {
    QueryPerformanceCounter(&start_);
    }

/**
\brief reset timer to 0
*/
OpenGLTrainer::Timer::Seconds OpenGLTrainer::Timer::elapsed() const
    {
#ifdef _WIN32
    Win32TimeInteger now_;
    QueryPerformanceCounter(&now_);
    return double (now_.QuadPart - start_.QuadPart) / double(frequency.QuadPart);
#else
    #error Unix Version Not fully implemented yet!
#endif
    }

/**
\brief stop current lap timer, record interval in lastLapInternal and start new lap timer
*/
void OpenGLTrainer::Timer::lap()
    {
#ifdef _WIN32
    LARGE_INTEGER last=lastLap_;
    QueryPerformanceCounter(&lastLap_);
    lastLapInterval_ = double (lastLap_.QuadPart - last.QuadPart) / double(frequency.QuadPart);
#else
    clock_t last=lastLap_;
    lastLap_ = clock();
    lastLapInterval_ =(double)(now - lastTime)/CLK_TCK;
#endif
    }

/*******************************************************************************
    Internal Functions & Internal Class Member Functions 
*******************************************************************************/
/* NONE */
/*******************************************************************************
    Private Functions & Private Class Member Functions 
*******************************************************************************/
/* NONE */